/*
 * Problem: 02食物链（Food Chain）
 * Author: Yuanshun L
 * Coded: 19-nov-2021
 * Topic: 并查集
 */

#include<iostream>

using namespace std;

const int maxn = 3 * 50000;
int par[maxn]; // x,x+n,x+2n indicate A ，B，C groups respectively
int height[maxn]; // height

// unite-check set

// init
void init(int nn){
    for(int i=0;i<nn;i++){
        par[i] = i;
        height[i] = 0;
    }
}

// find the root of a node
int find2(int x){
    while(x != par[x]){
        x = par[x];
    }
    return x;
}

// unite two tree while x belongs a tree and y another
void unite(int x,int y){
    int rootx,rooty;
    rootx = find2(x);
    rooty = find2(y);
    if(rootx == rooty)
        return;
    if(height[rooty] < height[rootx])
        par[rooty] = rootx;
    else
        par[rootx] = rooty;
}

// judge if two node are in the same tree
bool same(int x,int y){
    return find2(x) == find2(y);
}

void solve(){
    int k,n,type; // n means the number of animals
    cin >> n >> k;
    init(n*3);
    int x,y,cnt=0;
    while(k--){
        cin >> type >> x >> y;
        if((x<1 ||  x>n) ||(y<1 || y>n)){
            cnt++;
            continue;
        }
        x--; y--;
        if(type == 1){ // belong to the common tree
            if(same(x,n+y) || same(x,2*n+y)){
                cnt++;
            }
            else{
                unite(x,y);
                unite(x+n,y+n);
                unite(x+2*n,y+2*n);
            }
        } // x eats y
        else{
            if(same(x,y) || same(x,2*n+y)){
                cnt++;
            }
            else{
                unite(x,n+y);
                unite(x+n,2*n+y);
                unite(x+2*n,y);
            }
        }
    }
    cout<<"The number of input error : " << cnt <<endl;
}

int main(){

    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    solve();

    return 0;
}